package com.ztjava.core.base;

import java.util.List;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.ehcache.CacheKit;
import com.ztjava.core.Constant;
import com.ztjava.core.util.DateUtil;

public class BaseController extends Controller {

	public static final String w_like = "like";
	public static final String w_left_like = "left_like";
	public static final String w_integer_eq = "integer_eq";
	public static final String w_integer_gt = "integer_gt";
	public static final String w_integer_lt = "integer_lt";
	public static final String w_string_eq = "string_eq";
	public static final String w_string_gt = "string_gt";
	public static final String w_string_lt = "string_lt";
	//自动组合查询参数
	public String paramToWhere(String[]... field){
		String where = " where 1=1";
		String value = "";
		String param = "";
		for(String[] item:field){
			value = getPara(item[0],"");
			if(!value.equals("")){
				if(item.length>2){
					param = item[2];
				}else {
					param = item[0];
				}
				if(item[1].equals(w_like)){
					where = where + " and "+param+" like '%"+value+"%'";
				}else if(item[1].equals(w_left_like)){
					where = where + " and "+param+" like '"+value+"%'";
				}else if(item[1].equals(w_string_eq)){
					where = where + " and "+param+"='"+value+"'";
				}else if(item[1].equals(w_integer_eq)){
					where = where + " and "+param+"="+value;
				}else if(item[1].equals(w_string_lt)){
					where = where + " and "+param+"<='"+value+"'";
				}else if(item[1].equals(w_string_gt)){
					where = where + " and "+param+">='"+value+"'";
				}
				keepPara(item[0]);
			}
		}
		return where;
	}
	
	//数据 的基础数据转换
	public void databaseChange(List list,String[]... rule){
		Object value;
		Map map;
		for(int i=0;i<list.size();i++){
			for(String[] item:rule){
				value =  ((Model)(list.get(i))).get(item[0]);
				map = CacheKit.get(Constant.CacheData, item[1]);
				if(value!=null&&map!=null&&map.get(value.toString())!=null){
					((Model)(list.get(i))).put(item[0]+"_cn", ((Record)map.get(value.toString())).get("name"));
				}
			}
		}
	}
	public void modelChange(Model model,String[]... fields){
		String value = "";
		Map map;
		for(int i=0;i<fields.length;i++){
			for(String[] item:fields){
				value = model.getStr(item[0]);
				map = CacheKit.get(Constant.CacheData, item[1]);
				if(value!=null&&map!=null&&map.get(value.toString())!=null){
					model.put(item[0]+"_cn", ((Record)map.get(value.toString())).get("name"));
				}
			}
			
		}
	}
	
	public String orderby(String pre,String orderField,String orderDirection){
		String orderby = " order by "+pre+getPara(orderField,"id")+" "+getPara(orderDirection,"desc");
		setAttr(orderField, getPara(orderField,"id"));
		setAttr(orderDirection, getPara(orderDirection,"desc"));
		return orderby;
	}
	public String orderby(String orderField,String orderDirection){
		return orderby("",orderField,orderDirection);
	}
	//自动保存Model
	public void saveModel(Model model,Integer user_id){
		if(model.get("id")!=null&&model.getInt("id")>0){
			model.set("update_time", DateUtil.getNowTime());
			model.set("update_user", user_id);
			model.update();
		}else {
			model.set("create_time", DateUtil.getNowTime());
			model.set("create_user", user_id);
			model.save();
		}
	}
	public void renderBackTpl(String path){
		render("/WEB-INF/tpl"+path);
	}
	public void renderFrontView(String path){
		render("/WEB-INF/view"+path);
	}

	public void renderSuccess(String message,Model<?> model){
		setAttr("result", "success");
		setAttr("message", message);
		setAttr("object", model);
		renderJson(new String[]{"result","message","object"});
	}
	public void renderSuccess(String message,Record record){
		setAttr("result", "success");
		setAttr("message", message);
		setAttr("object", record);
		renderJson(new String[]{"result","message","object"});
	}
	public void treeSort(List list,List newList,Integer parent_id,Integer level){
		Model m;
		for(int i=0;i<list.size();i++){
			m = (Model)list.get(i);
			if(m.getInt("parent_id").equals(parent_id)){
				((Model)list.get(i)).put("level",level);
				newList.add(list.get(i));
				treeSort(list,newList,m.getInt("id"),level+1);
			}
		}
	}
	
	//DwzUI需要的
	public void renderDwzSuccess(String message){
		renderDwz("200", message, "", "", "", "");
	}
	public void renderDwzDialogSuccess(){
		renderDwz("200", "操作成功!", "", "closeCurrent", "", "");
	}
	public void renderDwzDialogSuccess(String message){
		renderDwz("200", message, "", "closeCurrent", "", "");
	}
	public void renderDwzDialogError(String message){
		renderDwz("300", message, "", "", "", "");
	}
	public void renderDwzNavTabSuccess(){
		renderDwz("200", "操作成功!", "", "", "", "");
	}
	public void renderDwzNavTabSuccess(String message){
		renderDwz("200", message, "", "", "", "");
	}
	public void renderDwzNavTabError(String message){
		renderDwz("300", message, "", "", "", "");
	}
	public void renderDwz(String statusCode,String message,String navTabId,String callbackType,String forwardUrl,String confirmMsg){
		setAttr("statusCode", statusCode);
		setAttr("message", message);
		setAttr("navTabId", navTabId);
		setAttr("callbackType",callbackType);
		setAttr("forwardUrl", forwardUrl);
		setAttr("confirmMsg",confirmMsg);
		renderJson(new String[]{"statusCode","message","navTabId","callbackType","forwardUrl","confirmMsg"});
	}
	/****
	 * render
	 */
	public void renderError(String message){
		setAttr("result", "error");
		setAttr("message", message);
		renderJson(new String[]{"result","message"});
	}
	public void renderSuccess(){
		renderSuccess("操作成功！");
	}
	public void renderSuccess(String message){
		setAttr("result", "success");
		setAttr("message", message);
		renderJson(new String[]{"result","message"});
	}
	public void renderSuccess(boolean close,boolean refresh,String message){
		setAttr("result", "success");
		setAttr("message", message);
		if(close){
			setAttr("callbackType", "closeCurrent");
		}
		if(refresh){
			setAttr("refreshParent", true);
		}
		renderJson(new String[]{"result","message","callbackType","refreshParent"});
	}
	public void renderPageJson(Page pd){
		setAttr("total", pd.getTotalRow());
		setAttr("rows", pd.getList());
		renderJson(new String[]{"total","rows"});
	}
}
